home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / SCHEME / GNU / SCM4E1 / !Scm / slib / format < prev    next >
Text File  |  1994-03-05  |  57KB  |  1,680 lines

  1. ;;; format.scm 
  2. ;
  3. ; Common LISP text output formatter for SLIB
  4. ;
  5. ; Copyright (C) 1992-1994 by Dirk Lutzebaeck (lutzeb@cs.tu-berlin.de)
  6. ;
  7. ; Authors of the original version (< 1.4) were Ken Dickey and Aubrey Jaffer.
  8. ; Please send error reports to the email address above.
  9. ; For documentation see slib.texi and format.doc.
  10. ; For testing load formatst.scm.
  11. ;
  12. ; Version 3.0
  13.  
  14. (provide 'format)
  15. (require 'string-case)
  16. (require 'string-port)
  17. (require 'rev4-optional-procedures)
  18.  
  19. ;;; Configuration ------------------------------------------------------------
  20.  
  21. (define format:symbol-case-conv #f)
  22. ;; Symbols are converted by symbol->string so the case of the printed
  23. ;; symbols is implementation dependent. format:symbol-case-conv is a
  24. ;; one arg closure which is either #f (no conversion), string-upcase!,
  25. ;; string-downcase! or string-capitalize!.
  26.  
  27. (define format:iobj-case-conv #f)
  28. ;; As format:symbol-case-conv but applies for the representation of
  29. ;; implementation internal objects.
  30.  
  31. (define format:expch #\E)
  32. ;; The character prefixing the exponent value in ~e printing.
  33.  
  34. (define format:floats (provided? 'inexact))
  35. ;; Detects if the scheme system implements flonums (see at eof).
  36.  
  37. (define format:complex-numbers (provided? 'complex))
  38. ;; Detects if the scheme system implements complex numbers.
  39.  
  40. (define format:radix-pref (char=? #\# (string-ref (number->string 8 8) 0)))
  41. ;; Detects if number->string adds a radix prefix.
  42.  
  43. (define format:ascii-non-printable-charnames
  44.   '#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel"
  45.      "bs"  "ht"  "nl"  "vt"  "np"  "cr"  "so"  "si"
  46.      "dle" "dc1" "dc2" "dc3" "dc4" "nak" "syn" "etb"
  47.      "can" "em"  "sub" "esc" "fs"  "gs"  "rs"  "us" "space"))
  48.  
  49. ;;; End of configuration ----------------------------------------------------
  50.  
  51. (define format:version "3.0")
  52. (define format:port #f)            ; curr. format output port
  53. (define format:output-col 0)        ; curr. format output tty column
  54. (define format:flush-output #f)        ; flush output at end of formatting
  55. (define format:case-conversion #f)
  56. (define format:error-continuation #f)
  57. (define format:args #f)
  58. (define format:pos 0)            ; curr. format string parsing position
  59. (define format:arg-pos 0)        ; curr. format argument position
  60.                     ; this is global for error presentation
  61.  
  62. ; format string and char output routines on format:port
  63.  
  64. (define (format:out-str str)
  65.   (if format:case-conversion
  66.       (display (format:case-conversion str) format:port)
  67.       (display str format:port))
  68.   (set! format:output-col
  69.     (+ format:output-col (string-length str))))
  70.  
  71. (define (format:out-char ch)
  72.   (if format:case-conversion
  73.       (display (format:case-conversion (string ch)) format:port)
  74.       (write-char ch format:port))
  75.   (set! format:output-col
  76.     (if (char=? ch #\newline)
  77.         0
  78.         (+ format:output-col 1))))
  79.  
  80. ;(define (format:out-substr str i n)  ; this allocates a new string
  81. ;  (display (substring str i n) format:port)
  82. ;  (set! format:output-col (+ format:output-col n)))
  83.  
  84. (define (format:out-substr str i n)
  85.   (do ((k i (+ k 1)))
  86.       ((= k n))
  87.     (write-char (string-ref str k) format:port))
  88.   (set! format:output-col (+ format:output-col n)))
  89.  
  90. ;(define (format:out-fill n ch)       ; this allocates a new string
  91. ;  (format:out-str (make-string n ch)))
  92.  
  93. (define (format:out-fill n ch)
  94.   (do ((i 0 (+ i 1)))
  95.       ((= i n))
  96.     (write-char ch format:port))
  97.   (set! format:output-col (+ format:output-col n)))
  98.  
  99. ; format's user error handler
  100.  
  101. (define (format:error . args)        ; never returns!
  102.   (let ((error-continuation format:error-continuation)
  103.     (format-args format:args)
  104.     (port (current-error-port)))
  105.     (set! format:error format:intern-error)
  106.     (if (and (>= (length format:args) 2)
  107.          (string? (cadr format:args)))
  108.     (let ((format-string (cadr format-args)))
  109.       (if (not (zero? format:arg-pos))
  110.           (set! format:arg-pos (- format:arg-pos 1)))
  111.       (format port "~%FORMAT: error with call: (format ~a \"~a<===~a\" ~
  112.                                   ~{~a ~}===>~{~a ~})~%        "
  113.           (car format:args)
  114.           (substring format-string 0 format:pos)
  115.           (substring format-string format:pos
  116.                  (string-length format-string))
  117.           (list-head (cddr format:args) format:arg-pos)
  118.           (list-tail (cddr format:args) format:arg-pos)))
  119.     (format port 
  120.         "~%FORMAT: error with call: (format~{ ~a~})~%        "
  121.         format:args))
  122.     (apply format port args)
  123.     (newline port)
  124.     (set! format:error format:error-save)
  125.     (set! format:error-continuation error-continuation)
  126.     (format:abort)
  127.     (format:intern-error "format:abort does not jump to toplevel!")))
  128.  
  129. (define format:error-save format:error)
  130.  
  131. (define (format:intern-error . args)   ;if something goes wrong in format:error
  132.   (display "FORMAT: INTERNAL ERROR IN FORMAT:ERROR!") (newline)
  133.   (display "        format args: ") (write format:args) (newline)
  134.   (display "        error args:  ") (write args) (newline)
  135.   (set! format:error format:error-save)
  136.   (format:abort))
  137.  
  138. (define (format:format . args)        ; the formatter entry
  139.   (set! format:args args)
  140.   (set! format:arg-pos 0)
  141.   (set! format:pos 0)
  142.   (if (< (length args) 1)
  143.       (format:error "not enough arguments"))
  144.   (let ((destination (car args))
  145.     (arglist (cdr args)))
  146.     (cond
  147.      ((or (and (boolean? destination)    ; port output
  148.            destination)
  149.       (output-port? destination)
  150.       (number? destination))
  151.       (format:out (cond
  152.            ((boolean? destination) (current-output-port))
  153.            ((output-port? destination) destination)
  154.            ((number? destination) (current-error-port)))
  155.           (car arglist) (cdr arglist)))
  156.      ((and (boolean? destination)    ; string output
  157.        (not destination))
  158.       (call-with-output-string
  159.        (lambda (port) (format:out port (car arglist) (cdr arglist)))))
  160.      ((string? destination)        ; dest. is format string (Scheme->C)
  161.       (call-with-output-string
  162.        (lambda (port)
  163.      (format:out port destination arglist))))
  164.      (else
  165.       (format:error "illegal destination `~a'" destination)))))
  166.  
  167. (define (format:out port fmt args)    ; the output handler for a port
  168.   (set! format:port port)        ; global port for output routines
  169.   (set! format:case-conversion #f)    ; modifier case conversion procedure
  170.   (set! format:flush-output #f)        ; ~! reset
  171.   (let ((arg-pos (format:format-work fmt args))
  172.     (arg-len (length args)))
  173.     (cond
  174.      ((< arg-pos arg-len)
  175.       (set! format:arg-pos (+ arg-pos 1))
  176.       (set! format:pos (string-length fmt))
  177.       (format:error "~a superfluous argument~:p" (- arg-len arg-pos)))
  178.      ((> arg-pos arg-len)
  179.       (set! format:arg-pos (+ arg-len 1))
  180.       (display format:arg-pos)
  181.       (format:error "~a missing argument~:p" (- arg-pos arg-len)))
  182.      (else
  183.       (if format:flush-output (force-output port))
  184.       #t))))
  185.  
  186. (define format:parameter-characters
  187.   '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+ #\v #\# #\'))
  188.  
  189. (define (format:format-work format-string arglist) ; does the formatting work
  190.   (letrec
  191.       ((format-string-len (string-length format-string))
  192.        (arg-pos 0)            ; argument position in arglist
  193.        (arg-len (length arglist))    ; number of arguments
  194.        (modifier #f)            ; 'colon | 'at | 'colon-at | #f
  195.        (params '())            ; directive parameter list
  196.        (param-value-found #f)        ; a directive parameter value found
  197.        (conditional-nest 0)        ; conditional nesting level
  198.        (clause-pos 0)            ; last cond. clause beginning char pos
  199.        (clause-default #f)        ; conditional default clause string
  200.        (clauses '())            ; conditional clause string list
  201.        (conditional-type #f)        ; reflects the contional modifiers
  202.        (conditional-arg #f)        ; argument to apply the conditional
  203.        (iteration-nest 0)        ; iteration nesting level
  204.        (iteration-pos 0)        ; iteration string beginning char pos
  205.        (iteration-type #f)        ; reflects the iteration modifiers
  206.        (max-iterations #f)        ; maximum number of iterations
  207.        (recursive-pos-save format:pos)
  208.  
  209.        (next-char            ; gets the next char from format-string
  210.     (lambda ()
  211.       (let ((ch (peek-next-char)))
  212.         (set! format:pos (+ 1 format:pos))
  213.         ch)))
  214.  
  215.        (peek-next-char
  216.     (lambda ()
  217.       (if (>= format:pos format-string-len)
  218.           (format:error "illegal format string")
  219.           (string-ref format-string format:pos))))
  220.  
  221.        (one-positive-integer?
  222.     (lambda (params)
  223.       (cond
  224.        ((null? params) #f)
  225.        ((and (integer? (car params))
  226.          (>= (car params) 0)
  227.          (= (length params) 1)) #t)
  228.        (else (format:error "one positive integer parameter expected")))))
  229.  
  230.        (next-arg
  231.     (lambda ()
  232.       (if (>= arg-pos arg-len)
  233.           (begin
  234.         (set! format:arg-pos (+ arg-len 1))
  235.         (format:error "missing argument(s)")))
  236.       (add-arg-pos 1)
  237.       (list-ref arglist (- arg-pos 1))))
  238.  
  239.        (prev-arg
  240.     (lambda ()
  241.       (add-arg-pos -1)
  242.       (if (negative? arg-pos)
  243.           (format:error "missing backward argument(s)"))
  244.       (list-ref arglist arg-pos)))
  245.  
  246.        (rest-args
  247.     (lambda ()
  248.       (let loop ((l arglist) (k arg-pos)) ; list-tail definition
  249.         (if (= k 0) l (loop (cdr l) (- k 1))))))
  250.  
  251.        (add-arg-pos
  252.     (lambda (n) 
  253.       (set! arg-pos (+ n arg-pos))
  254.       (set! format:arg-pos arg-pos)))
  255.  
  256.        (anychar-dispatch        ; dispatches the format-string
  257.     (lambda ()
  258.       (if (>= format:pos format-string-len)
  259.           arg-pos            ; used for ~? continuance
  260.           (let ((char (next-char)))
  261.         (cond
  262.          ((char=? char #\~)
  263.           (set! modifier #f)
  264.           (set! params '())
  265.           (set! param-value-found #f)
  266.           (tilde-dispatch))
  267.          (else
  268.           (if (and (zero? conditional-nest)
  269.                (zero? iteration-nest))
  270.               (format:out-char char))
  271.           (anychar-dispatch)))))))
  272.  
  273.        (tilde-dispatch
  274.     (lambda ()
  275.       (cond
  276.        ((>= format:pos format-string-len)
  277.         (format:out-str "~")    ; tilde at end of string is just output
  278.         arg-pos)            ; used for ~? continuance
  279.        ((and (or (zero? conditional-nest)
  280.              (memv (peek-next-char) ; find conditional directives
  281.                (append '(#\[ #\] #\; #\: #\@ #\^)
  282.                    format:parameter-characters)))
  283.          (or (zero? iteration-nest)
  284.              (memv (peek-next-char) ; find iteration directives
  285.                (append '(#\{ #\} #\: #\@ #\^)
  286.                    format:parameter-characters))))
  287.         (case (char-upcase (next-char))
  288.  
  289.           ;; format directives
  290.  
  291.           ((#\A)            ; Any -- for humans
  292.            (set! format:read-proof (memq modifier '(colon colon-at)))
  293.            (format:out-obj-padded (memq modifier '(at colon-at))
  294.                       (next-arg) #f params)
  295.            (anychar-dispatch))
  296.           ((#\S)            ; Slashified -- for parsers
  297.            (set! format:read-proof (memq modifier '(colon colon-at)))
  298.            (format:out-obj-padded (memq modifier '(at colon-at))
  299.                       (next-arg) #t params)
  300.            (anychar-dispatch))
  301.           ((#\D)            ; Decimal
  302.            (format:out-num-padded modifier (next-arg) params 10)
  303.            (anychar-dispatch))
  304.           ((#\X)            ; Hexadecimal
  305.            (format:out-num-padded modifier (next-arg) params 16)
  306.            (anychar-dispatch))
  307.           ((#\O)            ; Octal
  308.            (format:out-num-padded modifier (next-arg) params 8)
  309.            (anychar-dispatch))
  310.           ((#\B)            ; Binary
  311.            (format:out-num-padded modifier (next-arg) params 2)
  312.            (anychar-dispatch))
  313.           ((#\R)
  314.            (if (null? params)
  315.            (format:out-obj-padded ; Roman, cardinal, ordinal numerals
  316.             #f
  317.             ((case modifier
  318.                ((at) format:num->roman)
  319.                ((colon-at) format:num->old-roman)
  320.                ((colon) format:num->ordinal)
  321.                (else format:num->cardinal))
  322.              (next-arg))
  323.             #f params)
  324.            (format:out-num-padded ; any Radix
  325.             modifier (next-arg) (cdr params) (car params)))
  326.            (anychar-dispatch))
  327.           ((#\F)            ; Fixed-format floating-point
  328.            (if format:floats
  329.            (format:out-fixed modifier (next-arg) params)
  330.            (format:out-str (number->string (next-arg))))
  331.            (anychar-dispatch))
  332.           ((#\E)            ; Exponential floating-point
  333.            (if format:floats
  334.            (format:out-expon modifier (next-arg) params)
  335.            (format:out-str (number->string (next-arg))))
  336.            (anychar-dispatch))
  337.           ((#\G)            ; General floating-point
  338.            (if format:floats
  339.            (format:out-general modifier (next-arg) params)
  340.            (format:out-str (number->string (next-arg))))
  341.            (anychar-dispatch))
  342.           ((#\$)            ; Dollars floating-point
  343.            (if format:floats
  344.            (format:out-dollar modifier (next-arg) params)
  345.            (format:out-str (number->string (next-arg))))
  346.            (anychar-dispatch))
  347.           ((#\I)            ; Complex numbers
  348.            (if (not format:complex-numbers)
  349.            (format:error
  350.             "complex numbers not supported by this scheme system"))
  351.            (let ((z (next-arg)))
  352.          (if (not (complex? z))
  353.              (format:error "argument not a complex number"))
  354.          (format:out-fixed modifier (real-part z) params)
  355.          (format:out-fixed 'at (imag-part z) params)
  356.          (format:out-char #\i))
  357.            (anychar-dispatch))
  358.           ((#\C)            ; Character
  359.            (let ((ch (if (one-positive-integer? params)
  360.                  (integer->char (car params))
  361.                  (next-arg))))
  362.          (if (not (char? ch)) (format:error "~~c expects a character"))
  363.          (case modifier
  364.            ((at)
  365.             (format:out-str (format:char->str ch)))
  366.            ((colon)
  367.             (let ((c (char->integer ch)))
  368.               (if (< c 0)
  369.               (set! c (+ c 256))) ; compensate complement impl.
  370.               (cond
  371.                ((< c #x20)    ; assumes that control chars are < #x20
  372.             (format:out-char #\^)
  373.             (format:out-char
  374.              (integer->char (+ c #x40))))
  375.                ((>= c #x7f)
  376.             (format:out-str "#\\")
  377.             (format:out-str
  378.              (if format:radix-pref
  379.                  (let ((s (number->string c 8)))
  380.                    (substring s 2 (string-length s)))
  381.                  (number->string c 8))))
  382.                (else
  383.             (format:out-char ch)))))
  384.            (else (format:out-char ch))))
  385.            (anychar-dispatch))
  386.           ((#\P)            ; Plural
  387.            (if (memq modifier '(colon colon-at))
  388.            (prev-arg))
  389.            (let ((arg (next-arg)))
  390.          (if (not (number? arg))
  391.              (format:error "~~p expects a number argument"))
  392.          (if (= arg 1)
  393.              (if (memq modifier '(at colon-at))
  394.              (format:out-char #\y))
  395.              (if (memq modifier '(at colon-at))
  396.              (format:out-str "ies")
  397.              (format:out-char #\s))))
  398.            (anychar-dispatch))
  399.           ((#\~)            ; Tilde
  400.            (if (one-positive-integer? params)
  401.            (format:out-fill (car params) #\~)
  402.            (format:out-char #\~))
  403.            (anychar-dispatch))
  404.           ((#\%)            ; Newline
  405.            (if (one-positive-integer? params)
  406.            (format:out-fill (car params) #\newline)
  407.            (format:out-char #\newline))
  408.            (set! format:output-col 0)
  409.            (anychar-dispatch))
  410.           ((#\&)            ; Fresh line
  411.            (if (one-positive-integer? params)
  412.            (begin
  413.              (if (> (car params) 0)
  414.              (format:out-fill (- (car params)
  415.                          (if (> format:output-col 0) 0 1))
  416.                       #\newline))
  417.              (set! format:output-col 0))
  418.            (if (> format:output-col 0)
  419.                (format:out-char #\newline)))
  420.            (anychar-dispatch))
  421.           ((#\_)            ; Space character
  422.            (if (one-positive-integer? params)
  423.            (format:out-fill (car params) #\space)
  424.            (format:out-char #\space))
  425.            (anychar-dispatch))
  426.           ((#\/)            ; Tabulator character
  427.            (if (one-positive-integer? params)
  428.            (format:out-fill (car params) slib:tab)
  429.            (format:out-char slib:tab))
  430.            (anychar-dispatch))
  431.           ((#\|)            ; Page seperator
  432.            (if (one-positive-integer? params)
  433.            (format:out-str (car params) slib:form-feed)
  434.            (format:out-char slib:form-feed))
  435.            (set! format:output-col 0)
  436.            (anychar-dispatch))
  437.           ((#\T)            ; Tabulate
  438.            (format:tabulate modifier params)
  439.            (anychar-dispatch))
  440.           ((#\Y)            ; Pretty-print
  441.            (require 'pretty-print)
  442.            (pretty-print (next-arg) format:port)
  443.            (set! format:output-col 0)
  444.            (anychar-dispatch))
  445.           ((#\? #\K)        ; Indirection (is "~K" in T-Scheme)
  446.            (cond
  447.         ((memq modifier '(colon colon-at))
  448.          (format:error "illegal modifier in ~~?"))
  449.         ((eq? modifier 'at)
  450.          (let* ((frmt (next-arg))
  451.             (args (rest-args)))
  452.            (add-arg-pos (format:format-work frmt args))))
  453.         (else
  454.          (let* ((frmt (next-arg))
  455.             (args (next-arg)))
  456.            (format:format-work frmt args))))
  457.            (anychar-dispatch))
  458.           ((#\!)            ; Flush output
  459.            (set! format:flush-output #t)
  460.            (anychar-dispatch))
  461.           ((#\newline)        ; Continuation lines
  462.            (if (eq? modifier 'at)
  463.            (format:out-char #\newline))
  464.            (if (< format:pos format-string-len)
  465.            (do ((ch (peek-next-char) (peek-next-char)))
  466.                ((or (not (char-whitespace? ch))
  467.                 (= format:pos (- format-string-len 1))))
  468.              (if (eq? modifier 'colon)
  469.              (format:out-char (next-char))
  470.              (next-char))))
  471.            (anychar-dispatch))
  472.           ((#\*)            ; Argument jumping
  473.            (case modifier
  474.          ((colon)        ; jump backwards
  475.           (if (one-positive-integer? params)
  476.               (do ((i 0 (+ i 1)))
  477.               ((= i (car params)))
  478.             (prev-arg))
  479.               (prev-arg)))
  480.          ((at)            ; jump absolute
  481.           (set! arg-pos (if (one-positive-integer? params)
  482.                     (car params) 0)))
  483.          ((colon-at)
  484.           (format:error "illegal modifier `:@' in ~~* directive"))
  485.          (else            ; jump forward
  486.           (if (one-positive-integer? params)
  487.               (do ((i 0 (+ i 1)))
  488.               ((= i (car params)))
  489.             (next-arg))
  490.               (next-arg))))
  491.            (anychar-dispatch))
  492.           ((#\()            ; Case conversion begin
  493.            (set! format:case-conversion
  494.              (case modifier
  495.                ((at) string-capitalize-first)
  496.                ((colon) string-capitalize)
  497.                ((colon-at) string-upcase)
  498.                (else string-downcase)))
  499.            (anychar-dispatch))
  500.           ((#\))            ; Case conversion end
  501.            (if (not format:case-conversion)
  502.            (format:error "missing ~~("))
  503.            (set! format:case-conversion #f)
  504.            (anychar-dispatch))
  505.           ((#\[)            ; Conditional begin
  506.            (set! conditional-nest (+ conditional-nest 1))
  507.            (cond
  508.         ((= conditional-nest 1)
  509.          (set! clause-pos format:pos)
  510.          (set! clause-default #f)
  511.          (set! clauses '())
  512.          (set! conditional-type
  513.                (case modifier
  514.              ((at) 'if-then)
  515.              ((colon) 'if-else-then)
  516.              ((colon-at) (format:error "illegal modifier in ~~["))
  517.              (else 'num-case)))
  518.          (set! conditional-arg
  519.                (if (one-positive-integer? params)
  520.                (car params)
  521.                (next-arg)))))
  522.            (anychar-dispatch))
  523.           ((#\;)                    ; Conditional separator
  524.            (if (zero? conditional-nest)
  525.            (format:error "~~; not in ~~[~~] conditional"))
  526.            (if (not (null? params))
  527.            (format:error "no parameter allowed in ~~;"))
  528.            (if (= conditional-nest 1)
  529.            (let ((clause-str
  530.               (cond
  531.                ((eq? modifier 'colon)
  532.                 (set! clause-default #t)
  533.                 (substring format-string clause-pos 
  534.                        (- format:pos 3)))
  535.                ((memq modifier '(at colon-at))
  536.                 (format:error "illegal modifier in ~~;"))
  537.                (else
  538.                 (substring format-string clause-pos
  539.                        (- format:pos 2))))))
  540.              (set! clauses (append clauses (list clause-str)))
  541.              (set! clause-pos format:pos)))
  542.            (anychar-dispatch))
  543.           ((#\])            ; Conditional end
  544.            (if (zero? conditional-nest) (format:error "missing ~~["))
  545.            (set! conditional-nest (- conditional-nest 1))
  546.            (if modifier
  547.            (format:error "no modifier allowed in ~~]"))
  548.            (if (not (null? params))
  549.            (format:error "no parameter allowed in ~~]"))
  550.            (cond
  551.         ((zero? conditional-nest)
  552.          (let ((clause-str (substring format-string clause-pos
  553.                           (- format:pos 2))))
  554.            (if clause-default
  555.                (set! clause-default clause-str)
  556.                (set! clauses (append clauses (list clause-str)))))
  557.          (case conditional-type
  558.            ((if-then)
  559.             (if conditional-arg
  560.             (format:format-work (car clauses)
  561.                         (list conditional-arg))))
  562.            ((if-else-then)
  563.             (add-arg-pos
  564.              (format:format-work (if conditional-arg
  565.                          (cadr clauses)
  566.                          (car clauses))
  567.                      (rest-args))))
  568.            ((num-case)
  569.             (if (or (not (integer? conditional-arg))
  570.                 (< conditional-arg 0))
  571.             (format:error "argument not a positive integer"))
  572.             (if (not (and (>= conditional-arg (length clauses))
  573.                   (not clause-default)))
  574.             (add-arg-pos
  575.              (format:format-work
  576.               (if (>= conditional-arg (length clauses))
  577.                   clause-default
  578.                   (list-ref clauses conditional-arg))
  579.               (rest-args))))))))
  580.            (anychar-dispatch))
  581.           ((#\{)            ; Iteration begin
  582.            (set! iteration-nest (+ iteration-nest 1))
  583.            (cond
  584.         ((= iteration-nest 1)
  585.          (set! iteration-pos format:pos)
  586.          (set! iteration-type
  587.                (case modifier
  588.              ((at) 'rest-args)
  589.              ((colon) 'sublists)
  590.              ((colon-at) 'rest-sublists)
  591.              (else 'list)))
  592.          (set! max-iterations (if (one-positive-integer? params)
  593.                      (car params) #f))))
  594.            (anychar-dispatch))
  595.           ((#\})            ; Iteration end
  596.            (if (zero? iteration-nest) (format:error "missing ~~{"))
  597.            (set! iteration-nest (- iteration-nest 1))
  598.            (case modifier
  599.          ((colon)
  600.           (if (not max-iterations) (set! max-iterations 1)))
  601.          ((colon-at at) (format:error "illegal modifier"))
  602.          (else (if (not max-iterations) (set! max-iterations 100))))
  603.            (if (not (null? params))
  604.            (format:error "no parameters allowed in ~~}"))
  605.            (if (zero? iteration-nest)
  606.          (let ((iteration-str
  607.             (substring format-string iteration-pos
  608.                    (- format:pos (if modifier 3 2)))))
  609.            (if (string=? iteration-str "")
  610.                (set! iteration-str (next-arg)))
  611.            (case iteration-type
  612.              ((list)
  613.               (let ((args (next-arg))
  614.                 (args-len 0))
  615.             (if (not (list? args))
  616.                 (format:error "expected a list argument"))
  617.             (set! args-len (length args))
  618.             (do ((arg-pos 0 (+ arg-pos
  619.                        (format:format-work
  620.                         iteration-str
  621.                         (list-tail args arg-pos))))
  622.                  (i 0 (+ i 1)))
  623.                 ((or (>= arg-pos args-len)
  624.                  (>= i max-iterations))))))
  625.              ((sublists)
  626.               (let ((args (next-arg))
  627.                 (args-len 0))
  628.             (if (not (list? args))
  629.                 (format:error "expected a list argument"))
  630.             (set! args-len (length args))
  631.             (do ((arg-pos 0 (+ arg-pos 1)))
  632.                 ((or (>= arg-pos args-len)
  633.                  (>= arg-pos max-iterations)))
  634.               (let ((sublist (list-ref args arg-pos)))
  635.                 (if (not (list? sublist))
  636.                 (format:error
  637.                  "expected a list of lists argument"))
  638.                 (format:format-work iteration-str sublist)))))
  639.              ((rest-args)
  640.               (let* ((args (rest-args))
  641.                  (args-len (length args))
  642.                  (usedup-args
  643.                   (do ((arg-pos 0 (+ arg-pos
  644.                          (format:format-work
  645.                           iteration-str
  646.                           (list-tail
  647.                            args arg-pos))))
  648.                    (i 0 (+ i 1)))
  649.                   ((or (>= arg-pos args-len)
  650.                        (>= i max-iterations))
  651.                    arg-pos))))
  652.             (add-arg-pos usedup-args)))
  653.              ((rest-sublists)
  654.               (let* ((args (rest-args))
  655.                  (args-len (length args))
  656.                  (usedup-args
  657.                   (do ((arg-pos 0 (+ arg-pos 1)))
  658.                   ((or (>= arg-pos args-len)
  659.                        (>= arg-pos max-iterations))
  660.                    arg-pos)
  661.                 (let ((sublist (list-ref args arg-pos)))
  662.                   (if (not (list? sublist))
  663.                       (format:error "expected list arguments"))
  664.                   (format:format-work iteration-str sublist)))))
  665.             (add-arg-pos usedup-args)))
  666.              (else (format:error "internal error in ~~}")))))
  667.            (anychar-dispatch))
  668.           ((#\^)            ; Up and out
  669.            (let* ((continue
  670.                (cond
  671.             ((not (null? params))
  672.              (not
  673.               (case (length params)
  674.                ((1) (zero? (car params)))
  675.                ((2) (= (list-ref params 0) (list-ref params 1)))
  676.                ((3) (<= (list-ref params 0)
  677.                     (list-ref params 1)
  678.                     (list-ref params 2)))
  679.                (else (format:error "too much parameters")))))
  680.             (format:case-conversion ; if conversion stop conversion
  681.              (set! format:case-conversion string-copy) #t)
  682.             ((= iteration-nest 1) #t)
  683.             ((= conditional-nest 1) #t)
  684.             ((>= arg-pos arg-len)
  685.              (set! format:pos format-string-len) #f)
  686.             (else #t))))
  687.          (if continue
  688.              (anychar-dispatch))))
  689.  
  690.           ;; format directive modifiers and parameters
  691.  
  692.           ((#\@)            ; `@' modifier
  693.            (if (eq? modifier 'colon-at)
  694.            (format:error "double `@' modifier"))
  695.            (set! modifier (if (eq? modifier 'colon) 'colon-at 'at))
  696.            (tilde-dispatch))
  697.           ((#\:)            ; `:' modifier
  698.            (if modifier (format:error "illegal `:' modifier position"))
  699.            (set! modifier 'colon)
  700.            (tilde-dispatch))
  701.           ((#\')            ; Character parameter
  702.            (if modifier (format:error "misplaced modifier"))
  703.            (set! params (append params (list (char->integer (next-char)))))
  704.            (set! param-value-found #t)
  705.            (tilde-dispatch))
  706.           ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+) ; num. paramtr
  707.            (if modifier (format:error "misplaced modifier"))
  708.            (let ((num-str-beg (- format:pos 1))
  709.              (num-str-end format:pos))
  710.          (do ((ch (peek-next-char) (peek-next-char)))
  711.              ((not (char-numeric? ch)))
  712.            (next-char)
  713.            (set! num-str-end (+ 1 num-str-end)))
  714.          (set! params
  715.                (append params
  716.                    (list (string->number
  717.                       (substring format-string
  718.                          num-str-beg
  719.                          num-str-end))))))
  720.            (set! param-value-found #t)
  721.            (tilde-dispatch))
  722.           ((#\V)            ; Variable parameter from next argum.
  723.            (if modifier (format:error "misplaced modifier"))
  724.            (set! params (append params (list (next-arg))))
  725.            (set! param-value-found #t)
  726.            (tilde-dispatch))
  727.           ((#\#)            ; Parameter is number of remaining args
  728.            (if modifier (format:error "misplaced modifier"))
  729.            (set! params (append params (list (length (rest-args)))))
  730.            (set! param-value-found #t)
  731.            (tilde-dispatch))
  732.           ((#\,)            ; Parameter separators
  733.            (if modifier (format:error "misplaced modifier"))
  734.            (if (not param-value-found)
  735.            (set! params (append params '(#f)))) ; append empty paramtr
  736.            (set! param-value-found #f)
  737.            (tilde-dispatch))
  738.           ((#\Q)            ; Inquiry messages
  739.            (if (eq? modifier 'colon)
  740.            (format:out-str format:version)
  741.            (let ((nl (string #\newline)))
  742.              (format:out-str
  743.               (string-append
  744.                "SLIB Common LISP format version " format:version nl
  745.                "  (C) copyright 1992-1994 by Dirk Lutzebaeck" nl
  746.                "  please send bug reports to `lutzeb@cs.tu-berlin.de'"
  747.                nl))))
  748.            (anychar-dispatch))
  749.           (else            ; Unknown tilde directive
  750.            (format:error "unknown control character `~c'"
  751.               (string-ref format-string (- format:pos 1))))))
  752.        (else (anychar-dispatch)))))) ; in case of conditional
  753.  
  754.     (set! format:pos 0)
  755.     (set! format:arg-pos 0)
  756.     (anychar-dispatch)            ; start the formatting
  757.     (set! format:pos recursive-pos-save)
  758.     arg-pos))                ; return the position in the arg. list
  759.  
  760. ;; format:obj->str returns a R4RS representation as a string of an arbitrary
  761. ;; scheme object.
  762. ;; First parameter is the object, second parameter is a boolean if the
  763. ;; representation should be slashified as `write' does.
  764. ;; It uses format:char->str which converts a character into
  765. ;; a slashified string as `write' does and which is implementation dependent.
  766. ;; It uses format:iobj->str to print out internal objects as
  767. ;; quoted strings so that the output can always be processed by (read)
  768.  
  769. (define (format:obj->str obj slashify)
  770.   (cond
  771.    ((string? obj)
  772.     (if slashify
  773.     (let ((obj-len (string-length obj)))
  774.       (string-append
  775.        "\""
  776.        (let loop ((i 0) (j 0))    ; taken from Marc Feeley's pp.scm
  777.          (if (= j obj-len)
  778.          (string-append (substring obj i j) "\"")
  779.          (let ((c (string-ref obj j)))
  780.            (if (or (char=? c #\\)
  781.                (char=? c #\"))
  782.                (string-append (substring obj i j) "\\"
  783.                       (loop j (+ j 1)))
  784.                (loop i (+ j 1))))))))
  785.     obj))
  786.    
  787.    ((boolean? obj) (if obj "#t" "#f"))
  788.    
  789.    ((number? obj) (number->string obj))
  790.  
  791.    ((symbol? obj) 
  792.     (if format:symbol-case-conv
  793.     (format:symbol-case-conv (symbol->string obj))
  794.     (symbol->string obj)))
  795.    
  796.    ((char? obj)
  797.     (if slashify
  798.     (format:char->str obj)
  799.     (string obj)))
  800.    
  801.    ((null? obj) "()")
  802.  
  803.    ((input-port? obj)
  804.     (format:iobj->str obj))
  805.    
  806.    ((output-port? obj)
  807.     (format:iobj->str obj))
  808.      
  809.    ((list? obj)
  810.     (string-append "("
  811.            (let loop ((obj-list obj))
  812.              (if (null? (cdr obj-list))
  813.              (format:obj->str (car obj-list) #t)
  814.              (string-append
  815.               (format:obj->str (car obj-list) #t)
  816.               " "
  817.               (loop (cdr obj-list)))))
  818.            ")"))
  819.  
  820.    ((pair? obj)
  821.     (string-append "("
  822.            (format:obj->str (car obj) #t)
  823.            " . "
  824.            (format:obj->str (cdr obj) #t)
  825.            ")"))
  826.    
  827.    ((vector? obj)
  828.     (string-append "#" (format:obj->str (vector->list obj) #t)))
  829.  
  830.    (else                ; only objects with an #<...> 
  831.     (format:iobj->str obj))))        ; representation should fall in here
  832.  
  833. ;; format:iobj->str reveals the implementation dependent representation of 
  834. ;; #<...> objects with the use of display and call-with-output-string.
  835. ;; If format:read-proof is set to #t the resulting string is additionally 
  836. ;; set into string quotes.
  837.  
  838. (define format:read-proof #f)
  839.  
  840. (define (format:iobj->str iobj)
  841.   (if (or format:read-proof
  842.       format:iobj-case-conv)
  843.       (string-append 
  844.        (if format:read-proof "\"" "")
  845.        (if format:iobj-case-conv
  846.        (format:iobj-case-conv
  847.         (call-with-output-string (lambda (p) (display iobj p))))
  848.        (call-with-output-string (lambda (p) (display iobj p))))
  849.        (if format:read-proof "\"" ""))
  850.       (call-with-output-string (lambda (p) (display iobj p)))))
  851.  
  852.  
  853. ;; format:char->str converts a character into a slashified string as
  854. ;; done by `write'. The procedure is dependent on the integer
  855. ;; representation of characters and assumes a character number according to
  856. ;; the ASCII character set.
  857.  
  858. (define (format:char->str ch)
  859.   (let ((int-rep (char->integer ch)))
  860.     (if (< int-rep 0)            ; if chars are [-128...+127]
  861.     (set! int-rep (+ int-rep 256)))
  862.     (string-append
  863.      "#\\"
  864.      (cond
  865.       ((char=? ch #\newline) "newline")
  866.       ((and (>= int-rep 0) (<= int-rep 32))
  867.        (vector-ref format:ascii-non-printable-charnames int-rep))
  868.       ((= int-rep 127) "del")
  869.       ((>= int-rep 128)        ; octal representation
  870.        (if format:radix-pref
  871.        (let ((s (number->string int-rep 8)))
  872.          (substring s 2 (string-length s)))
  873.        (number->string int-rep 8)))
  874.       (else (string ch))))))
  875.  
  876. (define format:space-ch (char->integer #\space))
  877. (define format:zero-ch (char->integer #\0))
  878.  
  879. (define (format:par pars length index default name)
  880.   (if (> length index)
  881.       (let ((par (list-ref pars index)))
  882.     (if par
  883.         (if name
  884.         (if (< par 0)
  885.             (format:error 
  886.              "~s parameter must be a positive integer" name)
  887.             par)
  888.         par)
  889.         default))
  890.       default))
  891.  
  892. (define (format:out-obj-padded pad-left obj slashify pars)
  893.   (if (null? pars)
  894.       (format:out-str (format:obj->str obj slashify))
  895.       (let ((l (length pars)))
  896.     (let ((mincol (format:par pars l 0 0 "mincol"))
  897.           (colinc (format:par pars l 1 1 "colinc"))
  898.           (minpad (format:par pars l 2 0 "minpad"))
  899.           (padchar (integer->char
  900.             (format:par pars l 3 format:space-ch #f)))
  901.           (objstr (format:obj->str obj slashify)))
  902.       (if (not pad-left)
  903.           (format:out-str objstr))
  904.       (do ((objstr-len (string-length objstr))
  905.            (i minpad (+ i colinc)))
  906.           ((>= (+ objstr-len i) mincol)
  907.            (format:out-fill i padchar)))
  908.       (if pad-left
  909.           (format:out-str objstr))))))
  910.  
  911. (define (format:out-num-padded modifier number pars radix)
  912.   (if (not (integer? number)) (format:error "argument not an integer"))
  913.   (let ((numstr (number->string number radix)))
  914.     (if (and format:radix-pref (not (= radix 10)))
  915.     (set! numstr (substring numstr 2 (string-length numstr))))
  916.     (if (and (null? pars) (not modifier))
  917.     (format:out-str numstr)
  918.     (let ((l (length pars))
  919.           (numstr-len (string-length numstr)))
  920.       (let ((mincol (format:par pars l 0 #f "mincol"))
  921.         (padchar (integer->char
  922.               (format:par pars l 1 format:space-ch #f)))
  923.         (commachar (integer->char
  924.                 (format:par pars l 2 (char->integer #\,) #f)))
  925.         (commawidth (format:par pars l 3 3 "commawidth")))
  926.         (if mincol
  927.         (let ((numlen numstr-len)) ; calc. the output len of number
  928.           (if (and (memq modifier '(at colon-at)) (> number 0))
  929.               (set! numlen (+ numlen 1)))
  930.           (if (memq modifier '(colon colon-at))
  931.               (set! numlen (+ (quotient (- numstr-len 
  932.                            (if (< number 0) 2 1))
  933.                         commawidth)
  934.                       numlen)))
  935.           (if (> mincol numlen)
  936.               (format:out-fill (- mincol numlen) padchar))))
  937.         (if (and (memq modifier '(at colon-at))
  938.              (> number 0))
  939.         (format:out-char #\+))
  940.         (if (memq modifier '(colon colon-at)) ; insert comma character
  941.         (let ((start (remainder numstr-len commawidth))
  942.               (ns (if (< number 0) 1 0)))
  943.           (format:out-substr numstr 0 start)
  944.           (do ((i start (+ i commawidth)))
  945.               ((>= i numstr-len))
  946.             (if (> i ns)
  947.             (format:out-char commachar))
  948.             (format:out-substr numstr i (+ i commawidth))))
  949.         (format:out-str numstr)))))))
  950.  
  951. (define (format:tabulate modifier pars)
  952.   (let ((l (length pars)))
  953.     (let ((colnum (format:par pars l 0 1 "colnum"))
  954.       (colinc (format:par pars l 1 1 "colinc"))
  955.       (padch (integer->char (format:par pars l 2 format:space-ch #f))))
  956.       (case modifier
  957.     ((colon colon-at)
  958.      (format:error "unsupported modifier for ~~t"))
  959.     ((at)                ; relative tabulation
  960.      (format:out-fill
  961.       (if (= colinc 0)
  962.           colnum            ; colnum = colrel
  963.           (do ((c 0 (+ c colinc))
  964.            (col (+ format:output-col colnum)))
  965.           ((>= c col)
  966.            (- c format:output-col))))
  967.       padch))
  968.     (else                ; absolute tabulation
  969.      (format:out-fill
  970.       (cond
  971.        ((< format:output-col colnum)
  972.         (- colnum format:output-col))
  973.        ((= colinc 0)
  974.         0)
  975.        (else
  976.         (do ((c colnum (+ c colinc)))
  977.         ((>= c format:output-col)
  978.          (- c format:output-col)))))
  979.       padch))))))
  980.  
  981.  
  982. ;; roman numerals (from dorai@cs.rice.edu).
  983.  
  984. (define format:roman-alist
  985.   '((1000 #\M) (500 #\D) (100 #\C) (50 #\L)
  986.     (10 #\X) (5 #\V) (1 #\I)))
  987.  
  988. (define format:roman-boundary-values
  989.   '(100 100 10 10 1 1 #f))
  990.  
  991. (define format:num->old-roman
  992.   (lambda (n)
  993.     (if (and (integer? n) (>= n 1))
  994.     (let loop ((n n)
  995.            (romans format:roman-alist)
  996.            (s '()))
  997.       (if (null? romans) (list->string (reverse s))
  998.           (let ((roman-val (caar romans))
  999.             (roman-dgt (cadar romans)))
  1000.         (do ((q (quotient n roman-val) (- q 1))
  1001.              (s s (cons roman-dgt s)))
  1002.             ((= q 0)
  1003.              (loop (remainder n roman-val)
  1004.                (cdr romans) s))))))
  1005.     (format:error "only positive integers can be romanized"))))
  1006.  
  1007. (define format:num->roman
  1008.   (lambda (n)
  1009.     (if (and (integer? n) (> n 0))
  1010.     (let loop ((n n)
  1011.            (romans format:roman-alist)
  1012.            (boundaries format:roman-boundary-values)
  1013.            (s '()))
  1014.       (if (null? romans)
  1015.           (list->string (reverse s))
  1016.           (let ((roman-val (caar romans))
  1017.             (roman-dgt (cadar romans))
  1018.             (bdry (car boundaries)))
  1019.         (let loop2 ((q (quotient n roman-val))
  1020.                 (r (remainder n roman-val))
  1021.                 (s s))
  1022.           (if (= q 0)
  1023.               (if (and bdry (>= r (- roman-val bdry)))
  1024.               (loop (remainder r bdry) (cdr romans)
  1025.                 (cdr boundaries)
  1026.                 (cons roman-dgt
  1027.                   (append
  1028.                 (cdr (assv bdry romans))
  1029.                 s)))
  1030.               (loop r (cdr romans) (cdr boundaries) s))
  1031.               (loop2 (- q 1) r (cons roman-dgt s)))))))
  1032.     (format:error "only positive integers can be romanized"))))
  1033.  
  1034. ;; cardinals & ordinals (from dorai@cs.rice.edu)
  1035.  
  1036. (define format:cardinal-ones-list
  1037.   '(#f "one" "two" "three" "four" "five"
  1038.      "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen"
  1039.      "fourteen" "fifteen" "sixteen" "seventeen" "eighteen"
  1040.      "nineteen"))
  1041.  
  1042. (define format:cardinal-tens-list
  1043.   '(#f #f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
  1044.      "ninety"))
  1045.  
  1046. (define format:num->cardinal999
  1047.   (lambda (n)
  1048.     ;this procedure is inspired by the Bruno Haible's CLisp
  1049.     ;function format-small-cardinal, which converts numbers
  1050.     ;in the range 1 to 999, and is used for converting each
  1051.     ;thousand-block in a larger number
  1052.     (let* ((hundreds (quotient n 100))
  1053.        (tens+ones (remainder n 100))
  1054.        (tens (quotient tens+ones 10))
  1055.        (ones (remainder tens+ones 10)))
  1056.       (append
  1057.     (if (> hundreds 0)
  1058.         (append
  1059.           (string->list
  1060.         (list-ref format:cardinal-ones-list hundreds))
  1061.           (string->list" hundred")
  1062.           (if (> tens+ones 0) '(#\space) '()))
  1063.         '())
  1064.     (if (< tens+ones 20)
  1065.         (if (> tens+ones 0)
  1066.         (string->list
  1067.           (list-ref format:cardinal-ones-list tens+ones))
  1068.         '())
  1069.         (append
  1070.           (string->list
  1071.         (list-ref format:cardinal-tens-list tens))
  1072.           (if (> ones 0)
  1073.           (cons #\-
  1074.             (string->list
  1075.               (list-ref format:cardinal-ones-list ones))))))))))
  1076.  
  1077. (define format:cardinal-thousand-block-list
  1078.   '("" " thousand" " million" " billion" " trillion" " quadrillion"
  1079.      " quintillion" " sextillion" " septillion" " octillion" " nonillion"
  1080.      " decillion" " undecillion" " duodecillion" " tredecillion"
  1081.      " quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
  1082.      " octodecillion" " novemdecillion" " vigintillion"))
  1083.  
  1084. (define format:num->cardinal
  1085.   (lambda (n)
  1086.     (cond ((not (integer? n))
  1087.        (format:error
  1088.          "only integers can be converted to English cardinals"))
  1089.       ((= n 0) "zero")
  1090.       ((< n 0) (string-append "minus " (format:num->cardinal (- n))))
  1091.       (else
  1092.         (let ((power3-word-limit
  1093.             (length format:cardinal-thousand-block-list)))
  1094.           (let loop ((n n)
  1095.              (power3 0)
  1096.              (s '()))
  1097.         (if (= n 0)
  1098.             (list->string s)
  1099.             (let ((n-before-block (quotient n 1000))
  1100.               (n-after-block (remainder n 1000)))
  1101.               (loop n-before-block
  1102.             (+ power3 1)
  1103.             (if (> n-after-block 0)
  1104.                 (append
  1105.                   (if (> n-before-block 0)
  1106.                   (string->list ", ") '())
  1107.                   (format:num->cardinal999 n-after-block)
  1108.                   (if (< power3 power3-word-limit)
  1109.                   (string->list
  1110.                     (list-ref
  1111.                      format:cardinal-thousand-block-list
  1112.                      power3))
  1113.                   (append
  1114.                     (string->list " times ten to the ")
  1115.                     (string->list
  1116.                       (format:num->ordinal
  1117.                     (* power3 3)))
  1118.                     (string->list " power")))
  1119.                   s)
  1120.                 s))))))))))
  1121.  
  1122. (define format:ordinal-ones-list
  1123.   '(#f "first" "second" "third" "fourth" "fifth"
  1124.      "sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"
  1125.      "thirteenth" "fourteenth" "fifteenth" "sixteenth" "seventeenth"
  1126.      "eighteenth" "nineteenth"))
  1127.  
  1128. (define format:ordinal-tens-list
  1129.   '(#f #f "twentieth" "thirtieth" "fortieth" "fiftieth" "sixtieth"
  1130.      "seventieth" "eightieth" "ninetieth"))
  1131.  
  1132. (define format:num->ordinal
  1133.   (lambda (n)
  1134.     (cond ((not (integer? n))
  1135.        (format:error
  1136.          "only integers can be converted to English ordinals"))
  1137.       ((= n 0) "zeroth")
  1138.       ((< n 0) (string-append "minus " (format:num->ordinal (- n))))
  1139.       (else
  1140.         (let ((hundreds (quotient n 100))
  1141.           (tens+ones (remainder n 100)))
  1142.           (string-append
  1143.         (if (> hundreds 0)
  1144.             (string-append
  1145.               (format:num->cardinal (* hundreds 100))
  1146.               (if (= tens+ones 0) "th" " "))
  1147.             "")
  1148.         (if (= tens+ones 0) ""
  1149.             (if (< tens+ones 20)
  1150.             (list-ref format:ordinal-ones-list tens+ones)
  1151.             (let ((tens (quotient tens+ones 10))
  1152.                   (ones (remainder tens+ones 10)))
  1153.               (if (= ones 0)
  1154.                   (list-ref format:ordinal-tens-list tens)
  1155.                   (string-append
  1156.                 (list-ref format:cardinal-tens-list tens)
  1157.                 "-"
  1158.                 (list-ref format:ordinal-ones-list ones))))
  1159.             ))))))))
  1160.  
  1161. ;; format fixed flonums (~F)
  1162.  
  1163. (define (format:out-fixed modifier number pars)
  1164.   (if (not (or (number? number) (string? number)))
  1165.       (format:error "argument is not a number or a number string"))
  1166.  
  1167.   (let ((l (length pars)))
  1168.     (let ((width (format:par pars l 0 #f "width"))
  1169.       (digits (format:par pars l 1 #f "digits"))
  1170.       (scale (format:par pars l 2 0 #f))
  1171.       (overch (format:par pars l 3 #f #f))
  1172.       (padch (format:par pars l 4 format:space-ch #f)))
  1173.  
  1174.     (if digits
  1175.  
  1176.     (begin                ; fixed precision
  1177.       (format:parse-float 
  1178.        (if (string? number) number (number->string number)) #t scale)
  1179.       (if (<= (- format:fn-len format:fn-dot) digits)
  1180.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1181.           (format:fn-round digits))
  1182.       (if width
  1183.           (let ((numlen (+ format:fn-len 1)))
  1184.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1185.             (set! numlen (+ numlen 1)))
  1186.         (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1187.             (set! numlen (+ numlen 1)))
  1188.         (if (< numlen width)
  1189.             (format:out-fill (- width numlen) (integer->char padch)))
  1190.         (if (and overch (> numlen width))
  1191.             (format:out-fill width (integer->char overch))
  1192.             (format:fn-out modifier (> width (+ digits 1)))))
  1193.           (format:fn-out modifier #t)))
  1194.  
  1195.     (begin                ; free precision
  1196.       (format:parse-float
  1197.        (if (string? number) number (number->string number)) #t scale)
  1198.       (format:fn-strip)
  1199.       (if width
  1200.           (let ((numlen (+ format:fn-len 1)))
  1201.         (if (or (not format:fn-pos?) (eq? modifier 'at))
  1202.             (set! numlen (+ numlen 1)))
  1203.         (if (= format:fn-dot 0)
  1204.             (set! numlen (+ numlen 1)))
  1205.         (if (< numlen width)
  1206.             (format:out-fill (- width numlen) (integer->char padch)))
  1207.         (if (> numlen width)    ; adjust precision if possible
  1208.             (let ((dot-index (- numlen
  1209.                     (- format:fn-len format:fn-dot))))
  1210.               (if (> dot-index width)
  1211.               (if overch    ; numstr too big for required width
  1212.                   (format:out-fill width (integer->char overch))
  1213.                   (format:fn-out modifier #t))
  1214.               (begin
  1215.                 (format:fn-round (- width dot-index))
  1216.                 (format:fn-out modifier #t))))
  1217.             (format:fn-out modifier #t)))
  1218.           (format:fn-out modifier #t)))))))
  1219.  
  1220. ;; format exponential flonums (~E)
  1221.  
  1222. (define (format:out-expon modifier number pars)
  1223.   (if (not (or (number? number) (string? number)))
  1224.       (format:error "argument is not a number"))
  1225.  
  1226.   (let ((l (length pars)))
  1227.     (let ((width (format:par pars l 0 #f "width"))
  1228.       (digits (format:par pars l 1 #f "digits"))
  1229.       (edigits (format:par pars l 2 #f "exponent digits"))
  1230.       (scale (format:par pars l 3 1 #f))
  1231.       (overch (format:par pars l 4 #f #f))
  1232.       (padch (format:par pars l 5 format:space-ch #f))
  1233.       (expch (format:par pars l 6 #f #f)))
  1234.      
  1235.     (if digits                ; fixed precision
  1236.  
  1237.     (let ((digits (if (> scale 0)
  1238.               (if (< scale (+ digits 2))
  1239.                   (+ (- digits scale) 1)
  1240.                   0)
  1241.               digits)))
  1242.       (format:parse-float 
  1243.        (if (string? number) number (number->string number)) #f scale)
  1244.       (if (<= (- format:fn-len format:fn-dot) digits)
  1245.           (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1246.           (format:fn-round digits))
  1247.       (if width
  1248.           (if (and edigits overch (> format:en-len edigits))
  1249.           (format:out-fill width (integer->char overch))
  1250.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1251.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1252.             (set! numlen (+ numlen 1)))
  1253.             (if (and (= format:fn-dot 0) (> width (+ digits 1)))
  1254.             (set! numlen (+ numlen 1)))    
  1255.             (set! numlen
  1256.               (+ numlen 
  1257.                  (if (and edigits (>= edigits format:en-len))
  1258.                  edigits 
  1259.                  format:en-len)))
  1260.             (if (< numlen width)
  1261.             (format:out-fill (- width numlen)
  1262.                      (integer->char padch)))
  1263.             (if (and overch (> numlen width))
  1264.             (format:out-fill width (integer->char overch))
  1265.             (begin
  1266.               (format:fn-out modifier (> width (- numlen 1)))
  1267.               (format:en-out edigits expch)))))
  1268.           (begin
  1269.         (format:fn-out modifier #t)
  1270.         (format:en-out edigits expch))))
  1271.  
  1272.     (begin                ; free precision
  1273.       (format:parse-float
  1274.        (if (string? number) number (number->string number)) #f scale)
  1275.       (format:fn-strip)
  1276.       (if width
  1277.           (if (and edigits overch (> format:en-len edigits))
  1278.           (format:out-fill width (integer->char overch))
  1279.           (let ((numlen (+ format:fn-len 3))) ; .E+
  1280.             (if (or (not format:fn-pos?) (eq? modifier 'at))
  1281.             (set! numlen (+ numlen 1)))
  1282.             (if (= format:fn-dot 0)
  1283.             (set! numlen (+ numlen 1)))
  1284.             (set! numlen
  1285.               (+ numlen
  1286.                  (if (and edigits (>= edigits format:en-len))
  1287.                  edigits 
  1288.                  format:en-len)))
  1289.             (if (< numlen width)
  1290.             (format:out-fill (- width numlen)
  1291.                      (integer->char padch)))
  1292.             (if (> numlen width) ; adjust precision if possible
  1293.             (let ((f (- format:fn-len format:fn-dot))) ; fract len
  1294.               (if (> (- numlen f) width)
  1295.                   (if overch ; numstr too big for required width
  1296.                   (format:out-fill width 
  1297.                            (integer->char overch))
  1298.                   (begin
  1299.                     (format:fn-out modifier #t)
  1300.                     (format:en-out edigits expch)))
  1301.                   (begin
  1302.                 (format:fn-round (+ (- f numlen) width))
  1303.                 (format:fn-out modifier #t)
  1304.                 (format:en-out edigits expch))))
  1305.             (begin
  1306.               (format:fn-out modifier #t)
  1307.               (format:en-out edigits expch)))))
  1308.           (begin
  1309.         (format:fn-out modifier #t)
  1310.         (format:en-out edigits expch))))))))
  1311.     
  1312. ;; format general flonums (~G)
  1313.  
  1314. (define (format:out-general modifier number pars)
  1315.   (if (not (or (number? number) (string? number)))
  1316.       (format:error "argument is not a number or a number string"))
  1317.  
  1318.   (let ((l (length pars)))
  1319.     (let ((width (if (> l 0) (list-ref pars 0) #f))
  1320.       (digits (if (> l 1) (list-ref pars 1) #f))
  1321.       (edigits (if (> l 2) (list-ref pars 2) #f))
  1322.       (overch (if (> l 4) (list-ref pars 4) #f))
  1323.       (padch (if (> l 5) (list-ref pars 5) #f)))
  1324.     (format:parse-float
  1325.      (if (string? number) number (number->string number)) #t 0)
  1326.     (format:fn-strip)
  1327.     (let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
  1328.        (ww (if width (- width ee) #f))   ; see Steele's CL book p.395
  1329.        (n (if (= format:fn-dot 0)    ; number less than (abs 1.0) ?
  1330.           (- (format:fn-zlead))
  1331.           format:fn-dot))
  1332.        (d (if digits
  1333.           digits
  1334.           (max format:fn-len (min n 7)))) ; q = format:fn-len
  1335.        (dd (- d n)))
  1336.       (if (<= 0 dd d)
  1337.       (begin
  1338.         (format:out-fixed modifier number (list ww dd #f overch padch))
  1339.         (format:out-fill ee #\space)) ;~@T not implemented yet
  1340.       (format:out-expon modifier number pars))))))
  1341.  
  1342. ;; format dollar flonums (~$)
  1343.  
  1344. (define (format:out-dollar modifier number pars)
  1345.   (if (not (or (number? number) (string? number)))
  1346.       (format:error "argument is not a number or a number string"))
  1347.  
  1348.   (let ((l (length pars)))
  1349.     (let ((digits (format:par pars l 0 2 "digits"))
  1350.       (mindig (format:par pars l 1 1 "mindig"))
  1351.       (width (format:par pars l 2 0 "width"))
  1352.       (padch (format:par pars l 3 format:space-ch #f)))
  1353.  
  1354.     (format:parse-float
  1355.      (if (string? number) number (number->string number)) #t 0)
  1356.     (if (<= (- format:fn-len format:fn-dot) digits)
  1357.     (format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
  1358.     (format:fn-round digits))
  1359.     (let ((numlen (+ format:fn-len 1)))
  1360.       (if (or (not format:fn-pos?) (memq modifier '(at colon-at)))
  1361.       (set! numlen (+ numlen 1)))
  1362.       (if (and mindig (> mindig format:fn-dot))
  1363.       (set! numlen (+ numlen (- mindig format:fn-dot))))
  1364.       (if (and (= format:fn-dot 0) (not mindig))
  1365.       (set! numlen (+ numlen 1)))
  1366.       (if (< numlen width)
  1367.       (case modifier
  1368.         ((colon)
  1369.          (if (not format:fn-pos?)
  1370.          (format:out-char #\-))
  1371.          (format:out-fill (- width numlen) (integer->char padch)))
  1372.         ((at)
  1373.          (format:out-fill (- width numlen) (integer->char padch))
  1374.          (format:out-char (if format:fn-pos? #\+ #\-)))
  1375.         ((colon-at)
  1376.          (format:out-char (if format:fn-pos? #\+ #\-))
  1377.          (format:out-fill (- width numlen) (integer->char padch)))
  1378.         (else
  1379.          (format:out-fill (- width numlen) (integer->char padch))
  1380.          (if (not format:fn-pos?)
  1381.          (format:out-char #\-))))
  1382.       (if format:fn-pos?
  1383.           (if (memq modifier '(at colon-at)) (format:out-char #\+))
  1384.           (format:out-char #\-))))
  1385.     (if (and mindig (> mindig format:fn-dot))
  1386.     (format:out-fill (- mindig format:fn-dot) #\0))
  1387.     (if (and (= format:fn-dot 0) (not mindig))
  1388.     (format:out-char #\0))
  1389.     (format:out-substr format:fn-str 0 format:fn-dot)
  1390.     (format:out-char #\.)
  1391.     (format:out-substr format:fn-str format:fn-dot format:fn-len))))
  1392.  
  1393. ; the flonum buffers
  1394.  
  1395. (define format:fn-max 200)        ; max. number of number digits
  1396. (define format:fn-str (make-string format:fn-max)) ; number buffer
  1397. (define format:fn-len 0)        ; digit length of number
  1398. (define format:fn-dot #f)        ; dot position of number
  1399. (define format:fn-pos? #t)        ; number positive?
  1400. (define format:en-max 10)        ; max. number of exponent digits
  1401. (define format:en-str (make-string format:en-max)) ; exponent buffer
  1402. (define format:en-len 0)        ; digit length of exponent
  1403. (define format:en-pos? #t)        ; exponent positive?
  1404.  
  1405. (define (format:parse-float num-str fixed? scale)
  1406.   (set! format:fn-pos? #t)
  1407.   (set! format:fn-len 0)
  1408.   (set! format:fn-dot #f)
  1409.   (set! format:en-pos? #t)
  1410.   (set! format:en-len 0)
  1411.   (do ((i 0 (+ i 1))
  1412.        (left-zeros 0)
  1413.        (mantissa? #t)
  1414.        (all-zeros? #t)
  1415.        (num-len (string-length num-str))
  1416.        (c #f))            ; current exam. character in num-str
  1417.       ((= i num-len)
  1418.        (if (not format:fn-dot)
  1419.        (set! format:fn-dot format:fn-len))
  1420.  
  1421.        (if all-zeros?
  1422.        (begin
  1423.          (set! left-zeros 0)
  1424.          (set! format:fn-dot 0)
  1425.          (set! format:fn-len 1)))
  1426.  
  1427.        ;; now format the parsed values according to format's need
  1428.  
  1429.        (if fixed?
  1430.  
  1431.        (begin            ; fixed format m.nnn or .nnn
  1432.          (if (and (> left-zeros 0) (> format:fn-dot 0))
  1433.          (if (> format:fn-dot left-zeros) 
  1434.              (begin        ; norm 0{0}nn.mm to nn.mm
  1435.                (format:fn-shiftleft left-zeros)
  1436.                (set! left-zeros 0)
  1437.                (set! format:fn-dot (- format:fn-dot left-zeros)))
  1438.              (begin        ; normalize 0{0}.nnn to .nnn
  1439.                (format:fn-shiftleft format:fn-dot)
  1440.                (set! left-zeros (- left-zeros format:fn-dot))
  1441.                (set! format:fn-dot 0))))
  1442.          (if (or (not (= scale 0)) (> format:en-len 0))
  1443.          (let ((shift (+ scale (format:en-int))))
  1444.            (cond
  1445.             (all-zeros? #t)
  1446.             ((> (+ format:fn-dot shift) format:fn-len)
  1447.              (format:fn-zfill
  1448.               #f (- shift (- format:fn-len format:fn-dot)))
  1449.              (set! format:fn-dot format:fn-len))
  1450.             ((< (+ format:fn-dot shift) 0)
  1451.              (format:fn-zfill #t (- (- shift) format:fn-dot))
  1452.              (set! format:fn-dot 0))
  1453.             (else
  1454.              (if (> left-zeros 0)
  1455.              (if (<= left-zeros shift) ; shift always > 0 here
  1456.                  (format:fn-shiftleft shift) ; shift out 0s
  1457.                  (begin
  1458.                    (format:fn-shiftleft left-zeros)
  1459.                    (set! format:fn-dot (- shift left-zeros))))
  1460.              (set! format:fn-dot (+ format:fn-dot shift))))))))
  1461.  
  1462.        (let ((negexp        ; expon format m.nnnEee
  1463.           (if (> left-zeros 0)
  1464.               (- left-zeros format:fn-dot -1)
  1465.               (if (= format:fn-dot 0) 1 0))))
  1466.          (if (> left-zeros 0)
  1467.          (begin            ; normalize 0{0}.nnn to n.nn
  1468.            (format:fn-shiftleft left-zeros)
  1469.            (set! format:fn-dot 1))
  1470.          (if (= format:fn-dot 0)
  1471.              (set! format:fn-dot 1)))
  1472.          (format:en-set (- (+ (- format:fn-dot scale) (format:en-int))
  1473.                    negexp))
  1474.          (cond 
  1475.           (all-zeros?
  1476.            (format:en-set 0)
  1477.            (set! format:fn-dot 1))
  1478.           ((< scale 0)        ; leading zero
  1479.            (format:fn-zfill #t (- scale))
  1480.            (set! format:fn-dot 0))
  1481.           ((> scale format:fn-dot)
  1482.            (format:fn-zfill #f (- scale format:fn-dot))
  1483.            (set! format:fn-dot scale))
  1484.           (else
  1485.            (set! format:fn-dot scale)))))
  1486.        #t)
  1487.  
  1488.     ;; do body      
  1489.     (set! c (string-ref num-str i))    ; parse the output of number->string
  1490.     (cond                ; which can be any valid number
  1491.      ((char-numeric? c)            ; representation of R4RS except 
  1492.       (if mantissa?            ; complex numbers
  1493.       (begin
  1494.         (if (char=? c #\0)
  1495.         (if all-zeros?
  1496.             (set! left-zeros (+ left-zeros 1)))
  1497.         (begin
  1498.           (set! all-zeros? #f)))
  1499.         (string-set! format:fn-str format:fn-len c)
  1500.         (set! format:fn-len (+ format:fn-len 1)))
  1501.       (begin
  1502.         (string-set! format:en-str format:en-len c)
  1503.         (set! format:en-len (+ format:en-len 1)))))
  1504.      ((or (char=? c #\-) (char=? c #\+))
  1505.       (if mantissa?
  1506.       (set! format:fn-pos? (char=? c #\+))
  1507.       (set! format:en-pos? (char=? c #\+))))
  1508.      ((char=? c #\.)
  1509.       (set! format:fn-dot format:fn-len))
  1510.      ((char=? c #\e)
  1511.       (set! mantissa? #f))
  1512.      ((char=? c #\E)
  1513.       (set! mantissa? #f))
  1514.      ((char-whitespace? c) #t)
  1515.      ((char=? c #\d) #t)        ; decimal radix prefix
  1516.      ((char=? c #\#) #t)
  1517.      (else
  1518.       (format:error "illegal character `~c' in number->string" c)))))
  1519.  
  1520. (define (format:en-int)            ; convert exponent string to integer
  1521.   (if (= format:en-len 0)
  1522.       0
  1523.       (do ((i 0 (+ i 1))
  1524.        (n 0))
  1525.       ((= i format:en-len) 
  1526.        (if format:en-pos?
  1527.            n
  1528.            (- n)))
  1529.     (set! n (+ (* n 10) (- (char->integer (string-ref format:en-str i))
  1530.                    format:zero-ch))))))
  1531.  
  1532. (define (format:en-set en)        ; set exponent string number
  1533.   (set! format:en-len 0)
  1534.   (set! format:en-pos? (>= en 0))
  1535.   (let ((en-str (number->string en)))
  1536.     (do ((i 0 (+ i 1))
  1537.      (en-len (string-length en-str))
  1538.      (c #f))
  1539.     ((= i en-len))
  1540.       (set! c (string-ref en-str i))
  1541.       (if (char-numeric? c)
  1542.       (begin
  1543.         (string-set! format:en-str format:en-len c)
  1544.         (set! format:en-len (+ format:en-len 1)))))))
  1545.  
  1546. (define (format:fn-zfill left? n)    ; fill current number string with 0s
  1547.   (if (> (+ n format:fn-len) format:fn-max) ; from the left or right
  1548.       (format:error "number is too long to format (enlarge format:fn-max)"))
  1549.   (set! format:fn-len (+ format:fn-len n))
  1550.   (if left?
  1551.       (do ((i format:fn-len (- i 1)))    ; fill n 0s to left
  1552.       ((< i 0))
  1553.     (string-set! format:fn-str i
  1554.              (if (< i n)
  1555.              #\0
  1556.              (string-ref format:fn-str (- i n)))))
  1557.       (do ((i (- format:fn-len n) (+ i 1))) ; fill n 0s to the right
  1558.       ((= i format:fn-len))
  1559.     (string-set! format:fn-str i #\0))))
  1560.  
  1561. (define (format:fn-shiftleft n)        ; shift left current number n positions
  1562.   (if (> n format:fn-len)
  1563.       (format:error "internal error in format:fn-shiftleft (~d,~d)"
  1564.             n format:fn-len))
  1565.   (do ((i n (+ i 1)))
  1566.       ((= i format:fn-len)
  1567.        (set! format:fn-len (- format:fn-len n)))
  1568.     (string-set! format:fn-str (- i n) (string-ref format:fn-str i))))
  1569.  
  1570. (define (format:fn-round digits)    ; round format:fn-str
  1571.   (set! digits (+ digits format:fn-dot))
  1572.   (do ((i digits (- i 1))        ; "099",2 -> "10"
  1573.        (c 5))                ; "023",2 -> "02"
  1574.       ((or (= c 0) (< i 0))        ; "999",2 -> "100"
  1575.        (if (= c 1)            ; "005",2 -> "01"
  1576.        (begin            ; carry overflow
  1577.          (set! format:fn-len digits)
  1578.          (format:fn-zfill #t 1)    ; add a 1 before fn-str
  1579.          (string-set! format:fn-str 0 #\1)
  1580.          (set! format:fn-dot (+ format:fn-dot 1)))
  1581.        (set! format:fn-len digits)))
  1582.     (set! c (+ (- (char->integer (string-ref format:fn-str i))
  1583.           format:zero-ch) c))
  1584.     (string-set! format:fn-str i (integer->char
  1585.                   (if (< c 10) 
  1586.                       (+ c format:zero-ch)
  1587.                       (+ (- c 10) format:zero-ch))))
  1588.     (set! c (if (< c 10) 0 1))))
  1589.  
  1590. (define (format:fn-out modifier add-leading-zero?)
  1591.   (if format:fn-pos?
  1592.       (if (eq? modifier 'at) 
  1593.       (format:out-char #\+))
  1594.       (format:out-char #\-))
  1595.   (if (= format:fn-dot 0)
  1596.       (if add-leading-zero?
  1597.       (format:out-char #\0))
  1598.       (format:out-substr format:fn-str 0 format:fn-dot))
  1599.   (format:out-char #\.)
  1600.   (format:out-substr format:fn-str format:fn-dot format:fn-len))
  1601.  
  1602. (define (format:en-out edigits expch)
  1603.   (format:out-char (if expch (integer->char expch) format:expch))
  1604.   (format:out-char (if format:en-pos? #\+ #\-))
  1605.   (if edigits 
  1606.       (if (< format:en-len edigits)
  1607.       (format:out-fill (- edigits format:en-len) #\0)))
  1608.   (format:out-substr format:en-str 0 format:en-len))
  1609.  
  1610. (define (format:fn-strip)        ; strip trailing zeros but one
  1611.   (string-set! format:fn-str format:fn-len #\0)
  1612.   (do ((i format:fn-len (- i 1)))
  1613.       ((or (not (char=? (string-ref format:fn-str i) #\0))
  1614.        (<= i format:fn-dot))
  1615.        (set! format:fn-len (+ i 1)))))
  1616.  
  1617. (define (format:fn-zlead)        ; count leading zeros
  1618.   (do ((i 0 (+ i 1)))
  1619.       ((or (= i format:fn-len)
  1620.        (not (char=? (string-ref format:fn-str i) #\0)))
  1621.        (if (= i format:fn-len)        ; found a real zero
  1622.        0
  1623.        i))))
  1624.  
  1625.  
  1626. ;;; some global functions not found in SLIB
  1627.  
  1628. ;; string-index finds the index of the first occurence of the character `c'
  1629. ;; in the string `s'; it returns #f if there is no such character in `s'.
  1630.  
  1631. (define (string-index s c)
  1632.   (let ((slen-1 (- (string-length s) 1)))
  1633.     (let loop ((i 0))
  1634.       (cond
  1635.        ((char=? c (string-ref s i)) i)
  1636.        ((= i slen-1) #f)
  1637.        (else (loop (+ i 1)))))))
  1638.  
  1639. (define (string-capitalize-first str)    ; "hello" -> "Hello"
  1640.   (let ((cap-str (string-copy str))    ; "hELLO" -> "Hello"
  1641.     (non-first-alpha #f)        ; "*hello" -> "*Hello"
  1642.     (str-len (string-length str)))    ; "hello you" -> "Hello you"
  1643.     (do ((i 0 (+ i 1)))
  1644.     ((= i str-len) cap-str)
  1645.       (let ((c (string-ref str i)))
  1646.     (if (char-alphabetic? c)
  1647.         (if non-first-alpha
  1648.         (string-set! cap-str i (char-downcase c))
  1649.         (begin
  1650.           (set! non-first-alpha #t)
  1651.           (string-set! cap-str i (char-upcase c)))))))))
  1652.  
  1653. (define (list-head l k)
  1654.   (if (= k 0)
  1655.       '()
  1656.       (cons (car l) (list-head (cdr l) (- k 1)))))
  1657.  
  1658.  
  1659. ;; Aborts the program when a formatting error occures. This is a null
  1660. ;; argument closure to jump to the interpreters toplevel continuation.
  1661.  
  1662. (define format:abort (lambda () (slib:error "error in format")))
  1663.  
  1664. (define format format:format)
  1665.  
  1666. ;; If this is not possible then a continuation is used to recover
  1667. ;; properly from a format error. In this case format returns #f.
  1668.  
  1669. ;(define format:abort
  1670. ;  (lambda () (format:error-continuation #f)))
  1671.  
  1672. ;(define format
  1673. ;  (lambda args                ; wraps format:format with an error
  1674. ;    (call-with-current-continuation    ; continuation
  1675. ;     (lambda (cont)
  1676. ;       (set! format:error-continuation cont)
  1677. ;       (apply format:format args)))))
  1678.  
  1679. ;eof
  1680.